package com.ruoyi.project.pay;

import com.ruoyi.common.utils.SMSUtil;
import com.ruoyi.project.channel.channelAccount.domain.ChannelAccount;
import com.ruoyi.project.channel.channelAccount.service.IChannelAccountService;
import com.ruoyi.project.channel.smsInfo.service.ISmsInfoService;
import com.ruoyi.project.pay.mail.IMailService;
import com.ruoyi.project.system.user.domain.SysUser;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.ISysUserService;
import com.ruoyi.project.system.user.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class SecurityTimer {
    private Map<String,ArrayList<String>> nums = new HashMap<>();
    private static final Logger log = LoggerFactory.getLogger(SecurityTimer.class);
    private ISysUserService sysUserService;
    private IChannelAccountService channelAccountService;
    private IUserService userService;
    private int sum=0;
    private PayConfig payConfig;
    private ISmsInfoService smsInfoService;
    private IMailService mailService;
    private List<ChannelAccount> initAccounts ;

    public SecurityTimer(){
//        ArrayList<String> managerNums = new ArrayList<>();
//        managerNums.add("17759248105");
//        nums.put("manager",managerNums);

        ArrayList<String> bnNums = new ArrayList<>();
        bnNums.add("17759442185");
        bnNums.add("13509361447");
        bnNums.add("17850043961");
        nums.put("笨鸟支付",bnNums);

        ArrayList<String> bdtNums = new ArrayList<>();
        bdtNums.add("13459407741");
        bdtNums.add("13625092945");
        bdtNums.add("17067893302");
        bdtNums.add("18596699950");
        bdtNums.add("17067893057");
        nums.put("八达通支付",bdtNums);

        ArrayList<String> testNums = new ArrayList<>();
        testNums.add("17759248105");
        nums.put("测试后台",testNums);

        ArrayList<String> syNums = new ArrayList<>();
        syNums.add("15059014738");
        syNums.add("17850043961");
        syNums.add("13509361447");
        syNums.add("17068503937");
        syNums.add("17056246046 ");
        syNums.add("17056246114");
        nums.put("盛源支付",syNums);

        ArrayList<String> shNums = new ArrayList<>();
        shNums.add("15280092541");
        syNums.add("17850043961");
        nums.put("顺恒支付",shNums);
        sysUserService = SpringContextUtil.getBean(ISysUserService.class);
        channelAccountService = SpringContextUtil.getBean(IChannelAccountService.class);
        payConfig = SpringContextUtil.getBean(PayConfig.class);
        smsInfoService = SpringContextUtil.getBean(ISmsInfoService.class);
        userService = SpringContextUtil.getBean(IUserService.class);
        initAccounts = channelAccountService.selectChannelAccountList(new ChannelAccount());
        mailService = SpringContextUtil.getBean(IMailService.class);
//        if(payConfig.getNeedNotice().equals("noNeed")) {
//            return;
//        }

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    accounts();
                    SysUser searchC = new SysUser();
                    List<SysUser> sysUsers = sysUserService.selectSysUserList(searchC);
                    if(sysUsers.size() > 4){
                        sum++;
                        log.warn("系统被黑，请联系管理员");
                        sentMessage();
                        if(sum>=3){
                            System.exit(1);
                        }
                    }
                    for(int i=0;i<sysUsers.size();i++){
                        SysUser sysUser = sysUsers.get(i);
                        if(sysUser.getUser().equals("root") && !sysUser.getHost().equals("localhost")){
                            sum++;
                        }
                        if(sysUser.getUser().equals("mysql.session") && !sysUser.getHost().equals("localhost")){
                            sum++;
                        }
                        if(sysUser.getUser().equals("mysql.sys") && !sysUser.getHost().equals("localhost")){
                            sum++;
                        }
                        if(sysUser.getUser().equals("pay") && !sysUser.getHost().equals(payConfig.getWhiteHost())){
                            sum++;
                        }
                        if(!sysUser.getUser().equals("root") &&!sysUser.getUser().equals("mysql.session") &&!sysUser.getUser().equals("mysql.sys") &&!sysUser.getUser().equals("pay")){
                            sum++;
                        }
                    }
                    if(sum>0){
                        sentMessage();
                        log.warn("第几次警告："+sum);
                    }
                    if(sum>=3){
                        System.exit(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(),e);
                }
            }
        },30000,60 * 1000);
    }

    private void sentMessage(){
        String manNum = "17759248105";
//        SMSUtil.sendSms(payConfig.getWhiteHost()+"系统被黑，请联系管理员",manNum);
        String name = payConfig.getTitle();
        ArrayList<String> arrayList = nums.get(name);
        for(int i=0;i<arrayList.size();i++){
//            SMSUtil.sendSms(payConfig.getWhiteHost()+"系统被黑，请联系管理员",arrayList.get(i));
        }
    }

    private void accounts(){
        List<ChannelAccount> accounts = channelAccountService.selectChannelAccountList(new ChannelAccount());
        List<String> ps = nums.get(payConfig.getTitle());
        // 比对
        for(int i=0;i<accounts.size();i++){
            ChannelAccount account1 = accounts.get(i);
            boolean flag = false;
            for(int j=0;j<initAccounts.size();j++){
                ChannelAccount account2 = initAccounts.get(j);
                if(account1.getAccount().equals(account2.getAccount()) && account1.getPayType().equals(account2.getPayType())){
                    // 账号一致
                    flag = true;
                    if(!account1.getAppid().equals(account2.getAppid())){
                       User user = userService.selectUserById(account1.getChannelUserId());
                       String phoneNum = user.getPhonenumber();
                       if(ps.contains(phoneNum)){
//                           SMSUtil.sendSms("您修改了账号："+account1.getAccount()+",如有问题请联系管理员！",phoneNum);
                       }else{
                           log.info("账号被修改，并且对应的码商号码不存在！");
                           sentMessage();
                       }
                    }
                    break;
                }
            }
            if(!flag){
                // 新增的账号
                User user = userService.selectUserById(account1.getChannelUserId());
                String phoneNum = user.getPhonenumber();
                if(ps.contains(phoneNum)){
//                    SMSUtil.sendSms("您新增了账号："+account1.getAccount()+",如有问题请联系管理员！",phoneNum);
                }else{
                    log.info("新增账号，并且对应的码商号码不存在！");
                    sentMessage();
                }
            }
        }
        initAccounts = accounts;
    }
}
